var gulp    = require('gulp'),                 //基础库
    imagemin = require('gulp-imagemin'),       //图片压缩
    cssnano = require('gulp-cssnano'),         //css过滤
    minifycss = require('gulp-clean-css'),     //css压缩
    uglify  = require('gulp-uglify'),          //js压缩
    rename = require('gulp-rename'),           //重命名
    concat  = require('gulp-concat'),          //合并文件
    htmlmin  = require('gulp-htmlmin'),        //压缩html
    rev = require('gulp-rev'),                 //进行hash
    revCollector = require('gulp-rev-collector'), //- 路径替换
    clean = require('gulp-clean');            //删除
  // mygulp
  require('./gulp_temp');
  require('./gulp_pagejs');

var outPath = './star';
// var outPath = 'D://project/dev/wz.2035.info';
gulp.task('clean', function() {
	return gulp.src([outPath + '/css/*', outPath + '/js/*'])
	.pipe(clean());
});
// 广告主推广
gulp.task('minify-css', function() {
	return gulp.src('./src/css/*.css')
  .pipe(concat('core.css'))
	.pipe(cssnano())
	.pipe(minifycss({compatibility:'ie8'}))
	.pipe(rev())
	.pipe(gulp.dest(outPath+'/css'))
	.pipe(rev.manifest({path:'css.json', merge: true}))
	.pipe(gulp.dest('./rev'));
});

// 图片处理
gulp.task('images', function(){
    gulp.src('./src/images/*')
    .pipe(imagemin())
    .pipe(gulp.dest(outPath+'/images'));
})
gulp.task('icon', function(){
    gulp.src('./src/icon/**')
    .pipe(imagemin())
    .pipe(gulp.dest(outPath+'/icon'));
})

// 处理html
gulp.task('htmlmin', function () {
    var options = {
        removeComments: true,//清除HTML注释
        collapseWhitespace: true,//压缩HTML
        removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
        minifyJS: true,//压缩页面JS
        minifyCSS: true//压缩页面CSS
    };
    gulp.src(['./rev/*.json', './temp/*.html'])
	    .pipe(revCollector({
        replaceReved: true
      }))
      .pipe(htmlmin(options))
      .pipe(gulp.dest(outPath+'/page'));
});

//发布
gulp.task('build-all', ['includeHtml', 'minify-css', 'buildjs', 'images', 'icon'], () => {
  gulp.start('htmlmin');
});

gulp.task('build', ['includeHtml', 'minify-css', 'buildjs'], () => {
  gulp.start('htmlmin');
});
